import Singleton from "../base/Singleton";
import { BUNDLE_TYPE_ENUM, GAME_TYPE_ENUM } from "../base/EnumIndex";

export class ResManager extends Singleton {
    static get ins() {
        return super.getInstance<ResManager>();
    }

    private levelBundle: cc.AssetManager.Bundle = null;
    private gamePlayBundle: cc.AssetManager.Bundle = null;

    async loadGamePlayBundler() {
        if (!this.gamePlayBundle) {
            this.gamePlayBundle = await this.loadBundle("GamePlay");
        }
    }

    /** 加载关卡bundle */
    async loadLevelBundle(bundleName: string) {
        /** 加载下一关bundle时释放上一关bundle */
        if (this.levelBundle && this.levelBundle.name != bundleName) {
            this.releaseLevelBundle();
        }
        // let realBundleName = this.getBundleName(bundleName);
        this.levelBundle = await this.loadBundle(bundleName);
    }
    /** 释放关卡内bundle */
    releaseLevelBundle() {
        if (!this.levelBundle) return;
        console.log("release " + this.levelBundle.name);
        this.levelBundle.releaseAll();
        cc.assetManager.removeBundle(this.levelBundle);
        this.levelBundle = null;
    }

    /** 加载bundle */
    loadBundle(bundleName: string) {
        return new Promise<cc.AssetManager.Bundle>((resolve, reason) => {
            cc.assetManager.loadBundle(bundleName, (err, bundle) => {
                if (err) {
                    reason(err);
                    return;
                }
                resolve(bundle);
            });
        })
    }
    /** 预加载资源 */
    preloadRes(path: string | string[], type: typeof cc.Asset, bundleType: BUNDLE_TYPE_ENUM = BUNDLE_TYPE_ENUM.MAIN) {
        let bundle = this.getBundleByType(bundleType);
        if (!bundle) bundle = cc.resources;
        bundle.preload(path, type)
    }
    /** 加载单个资源 */
    loadRes<T extends cc.Asset>(path: string, type: { prototype: T }, bundleType: BUNDLE_TYPE_ENUM = BUNDLE_TYPE_ENUM.MAIN) {
        let bundle = this.getBundleByType(bundleType);
        if (!bundle) bundle = cc.resources;
        // console.log("loadRes", path, bundle.name)
        return new Promise<T>((resolve, reject) => {
            bundle.load(path, type, (err, res) => {
                if (err) {
                    reject(err);
                    return
                }
                resolve(res);
            });
        });
    }

    /** 根据bundleType 获取bundle */
    private getBundleByType(bundleType: BUNDLE_TYPE_ENUM) {
        switch (bundleType) {
            case BUNDLE_TYPE_ENUM.MAIN:
                return cc.resources;
            case BUNDLE_TYPE_ENUM.LEVEL:
                return this.levelBundle;
            case BUNDLE_TYPE_ENUM.GAME_PLAY:
                return this.gamePlayBundle;
            default:
                return cc.resources;
        }
    }

    // /** 获取实际的Bundle url */
    // private getBundleName(bundleName: string) {
    //     let realBundleName = bundleName;
    //     // 通过打包插件设置是否是本地测试
    //     let releaseType = window["local"] ? RELEASE_TYPE_ENUM.local : AdManager.releaseType;
    //     switch (releaseType) {
    //         /**本地测试 与 安卓  暂不使用远端bundle */
    //         case RELEASE_TYPE_ENUM.local:
    //         case RELEASE_TYPE_ENUM.android:
    //         case RELEASE_TYPE_ENUM.android_233:
    //         case RELEASE_TYPE_ENUM.h5_4399:
    //             break;
    //         default:
    //             let urlPrefix = '';
    //             switch (this.gameType) {
    //                 case GAME_TYPE_ENUM.NAO_DONG_TIAN_HUA_BAN:
    //                     urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/NaoDongTianHuaBan/';
    //                     break;
    //                 case GAME_TYPE_ENUM.WO_YAO_BIAN_TU_LE:
    //                     urlPrefix = 'https://naodthbdl.xmqixiang.cn/';
    //                     // urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/WoYaoHuiLaoJia/';
    //                     break;
    //                 case GAME_TYPE_ENUM.WEN_ZI_WO_FU_LE:
    //                     urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/WenZiWoFuLe/';
    //                     if (AdManager.gameId === 14 || AdManager.gameId === 8) {
    //                         urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/WenZiWoFuLe/ZhengHuoDaHangJia/';
    //                     }
    //                     break;
    //                 case GAME_TYPE_ENUM.WAN_DAN_ZHANG_NAO_ZI_LE:
    //                     // urlPrefix = 'https://naodthbdl.xmqixiang.cn/WanDanZhangNaoZiLe/';
    //                     urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/XieYinDaShi/';
    //                     break;
    //                 case GAME_TYPE_ENUM.Luan_DOU_DA_BI_PIN:
    //                     urlPrefix = 'https://res.wqop2018.com/partner/sjzsl/lddbp/';
    //                     break;
    //                 case GAME_TYPE_ENUM.Zui_Qiang_Zhe_Mo:
    //                     urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/zuiQiangZheMo/';
    //                     break;
    //                 case GAME_TYPE_ENUM.NAO_LI_YONG_ZHE:
    //                     urlPrefix = 'https://nlyz-cdn.jinkezhexin.top/';
    //                     break;
    //                 default:
    //                     urlPrefix = 'https://cdngame.shuling.vip/BianJuDaShi/NaoDongTianHuaBan/';
    //                     break;
    //             }
    //             let url = urlPrefix + releaseType + '/levelBundle/';
    //             // if (releaseType === RELEASE_TYPE_ENUM.ks && this.gameType === GAME_TYPE_ENUM.WEN_ZI_WO_FU_LE) {
    //             //     // url = 'https://cdngame.shuling.vip/BianJuDaShi/ZhengHuoDaHangJia/ksgame_wenfu/levelBundle/';
    //             //     url = 'https://cdngame.shuling.vip/BianJuDaShi/WenZiWoFuLe/ksgame/levelBundle/';
    //             // } else 
    //             if (AdManager.gameId === 18) {
    //                 // url = 'https://cdngame.shuling.vip/BianJuDaShi/WoYaoBianTuLe/ksgame/levelBundle/';
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/ZheBoWoHenXiu/ksgame/levelBundle/';
    //             } else if (AdManager.gameId === 26) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/XieYinGengZhuan/ksgame/levelBundle/';
    //             } else if (AdManager.gameId === 23) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/WoYaoShangAn/ksgame/levelBundle/';
    //             } else if (AdManager.gameId === 25) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/XieYinDaShi/ksgame/levelBundle/';
    //             } else if (AdManager.gameId === 29) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/NaoDongTianHuaBan/ksgame/levelBundle/';
    //             } else if (AdManager.gameId === 30) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/ChaoCaiWoZuiKuang/ksgame/levelBundle/';
    //             }else if (AdManager.gameId === 40) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/NaoLiXiuQiLai/ksgame/levelBundle2/';
    //             }else if (AdManager.gameId === 13) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/WenZiWoFuLe/ZhengHuoDaHangJia/bytedance/levelBundle/';
    //             }else if (AdManager.gameId === 41) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/TuRanDeNaoDong/ksgame/levelBundle2/'
    //             }else if (AdManager.gameId === 42) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/KanWoLaiZhengHuo/ksgame/levelBundle/';
    //             }else if (AdManager.gameId === 35) {
    //                 url = 'https://cdngame.shuling.vip/BianJuDaShi/ZhenShiDaCongMing/ksgame/levelBundle2/';
    //             }
    //             realBundleName = url + bundleName;
    //             break;
    //     }
    //     return realBundleName;
    // };
}